<?php
namespace app\mini\controller\commons;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\model\Agent;
use app\common\model\Banner;
use app\common\model\Member;
use app\common\model\Shop;
use app\common\traits\InterfacesExceptionTrait;
use think\Env;

class Info extends Base
{
    use InterfacesExceptionTrait;

    /**
     * 获取分享信息
     * @return array
     * @throws \think\exception\DbException
     */
    public function share_info()
    {

        $shop_id = input('shop_id','');
        $member_id = input('member_id','');
        $data['shop_name'] = '';
        $data['member_name'] = '';
        $data['img_url'] = Env::get('app.app_host').'/uploads/static/share.png';
        if (!empty($shop_id)){
            $shop_info = Shop::where('id',$shop_id)->find();
            $data['shop_name'] = $shop_info['shop_name'];
            $data['img_url'] = $shop_info['logo'];
        }
        if (!empty($member_id)){
            $data['member_name'] = Member::where('id',$member_id)->value('member_name');
        }
        return $this->response($data);
    }


    /**
     * 获取代理商信息
     * @return array
     * @throws \think\exception\DbException
     */
    public function agent_info()
    {

        $code = input('code','');
        $data['type'] = 0;//绑定
        if (empty($code))
            throw new ApiException('代理商code码未传',404);
        $info = Agent::field('id,nickname,avatar')->where(['code'=>$code])->find();
        if (empty($info))
            throw new ApiException('代理商信息不存在',404);
        if (!empty($info['member_id'])){
            if ($info['member_id'] != MemberVo::instance()->getMemberId() ){
                throw new ApiException('该代理商已被其他用户绑定',404);
            }
            $data['type'] = 1;//转移
        }
        $data['agent_info'] = $info;
        return $this->response($data);
    }



    /**
     * 首页提示
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index_tip()
    {
        $info = Banner::where(['type'=>3,'status'=>1])->order('id desc')->find();
        $data = [];
        if (!empty($info)){
            $data['url'] = $info['url'];
        }
        return $this->response($data);
    }
}